using System;
using System.Collections;
using System.Collections.Generic;
using System.Html;
using System.Runtime.CompilerServices;
using Awaiter;
using Canvas.Scripts.ImageRotate;
using Nsb.Classes;
using ThemeChain;
using jQueryApi;

namespace Yoshi.Classes
{
    [IgnoreNamespace]
    public static class StaticBuild
    {
        [PreserveName, PreserveCase]
        public static void LoadCssAw(Await awp, string cssFiles)
        {
            if (!String.IsNullOrEmpty(cssFiles))
            {
                string[] libs = cssFiles.Split(",");
                Await awx = new Await();

                foreach (string lib in libs)
                {
                    awx.LoadCss(Uri.Smart(lib));
                }

                awx.Commit(awp);
            }
            else
                awp.Done();
        }

        [PreserveName, PreserveCase]
        public static void LoadLibsAw(Await awp, string scripts)
        {
            if (!String.IsNullOrEmpty(scripts))
            {
                string[] libs = scripts.Split(",");
                Await awx = new Await();

                foreach (string lib in libs)
                {
                    awx.LoadScript(Uri.Smart(lib));
                }

                awx.Commit(awp);
            }
            else
                awp.Done();
        }

        [PreserveName, PreserveCase]
        public static void BuildAction(string action, string libs, string css)
        {
            if (!ThemeYoshi.PageDone)
            {
                Window.SetTimeout(delegate { BuildAction(action, libs, css); }, 13);
                return;
            }

            new Await()
                .AddAw(LoadCssAw, css)
                .AddAw(LoadLibsAw, libs)
                .AddDx(delegate
                       {
                           if (!String.IsNullOrEmpty(action))
                           {
                               try
                               {
                                   Script.Eval(action + "()");
                               }
                               catch (Exception ex)
                               {
                                   Inform.Error("Slide build error: {0}", ex);
                               }
                           }
                       })
                .Commit();
        }

        [PreserveName, PreserveCase]
        public static void Tilt(Dictionary tilts)
        {
            foreach (KeyValuePair<string, double> p in tilts)
            {
                Script.Literal("console.log({0})", String.Format("tilting {0} by {1}", p.Key, p.Value));
                jQuery.Select("#" + p.Key).Plugin<IRot>().Rotate(new Dictionary("angle", p.Value));
            }
        }

        [PreserveName, PreserveCase]
        public static void Finish()
        {
            Window.SetTimeout(delegate { jQuery.Select("#PageGigs").FadeIn(300); }, 50);
        }
    }
}
